<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="https://gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.96.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Go Swagger &middot; gofulljs blog</title>
  <meta name="description" content="" />

  
  <link type="text/css" rel="stylesheet" href="https://gosc.gitee.io/sc/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="https://gosc.gitee.io/sc/css/poole.css">
  <link type="text/css" rel="stylesheet" href="https://gosc.gitee.io/sc/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="https://gosc.gitee.io/sc/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class=" ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="https://gosc.gitee.io/sc/"><h1>gofulljs blog</h1></a>
      <p class="lead">
       my all blog 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="https://gosc.gitee.io/sc/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2022. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>Go Swagger</h1>
  <time datetime=2022-04-18T11:02:51&#43;0800 class="post-date">Mon, Apr 18, 2022</time>
  <h2 id="简单-gin-swagger">简单 gin swagger</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> (
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">_</span> <span style="color:#e6db74">&#34;gin_swagger/docs&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#e6db74">&#34;github.com/gin-gonic/gin&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">ginSwagger</span> <span style="color:#e6db74">&#34;github.com/swaggo/gin-swagger&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#e6db74">&#34;github.com/swaggo/gin-swagger/swaggerFiles&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// @title Swagger Example API
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @version 1.0
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @description This is a sample server celler server.
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @termsOfService http://swagger.io/terms/
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// @contact.name www.xxxxxx.com
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @contact.url https://www.xxxxxx.com
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @contact.email xxxxxx@xx.me
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// @license.name Apache 2.0
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// @host 127.0.0.1:8080
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @BasePath /api/v1
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">r</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Default</span>()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">r</span>.<span style="color:#a6e22e">GET</span>(<span style="color:#e6db74">&#34;/swagger/*any&#34;</span>, <span style="color:#a6e22e">ginSwagger</span>.<span style="color:#a6e22e">WrapHandler</span>(<span style="color:#a6e22e">swaggerFiles</span>.<span style="color:#a6e22e">Handler</span>))
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">v1</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">r</span>.<span style="color:#a6e22e">Group</span>(<span style="color:#e6db74">&#34;/api/v1&#34;</span>)
</span></span><span style="display:flex;"><span>	{
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">v1</span>.<span style="color:#a6e22e">GET</span>(<span style="color:#e6db74">&#34;/hello&#34;</span>, <span style="color:#a6e22e">HandleHello</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">r</span>.<span style="color:#a6e22e">Run</span>(<span style="color:#e6db74">&#34;:8080&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">HandleHello</span>(<span style="color:#a6e22e">ctx</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">ctx</span>.<span style="color:#a6e22e">String</span>(<span style="color:#ae81ff">200</span>, <span style="color:#e6db74">&#34;hello&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>信息参考:
<a href="https://swagger.io/specification/v2/#info-object">https://swagger.io/specification/v2/#info-object</a></p>
<p>预览:</p>
<p><img src="../../../../../../sc/20220418112848.png" alt=""></p>

</div>


    </main>

    
      
    
  </body>
</html>
